// pages/search/searchList/searchLIst.js
const common = require('../../../utils/util.js')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        sort: null,
        keyWord: null,
        goodsList: [],
        nowPage: 0,
        isLoading: false,
        isAll: false,
        shareImg: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        // if (options.storeId) {
        //     wx.setStorageSync('storeId', options.storeId)
        //     if (wx.getStorageSync('token')) {
        //         let data = {
        //             store_id: options.storeId
        //         }
        //         common.post("Store/change_user_store", data).then(function(res) {}).catch(function(e) {})
        //     }
        // }
        if (options.userShare) {
            wx.setStorageSync('userShare', options.userShare)
            let data = {
                belong_refer: options.userShare
            }
            common.post('User/set_belong_refer', data).then(function(res) {}).catch(function(e) {})
        }
        if (options.sort) {
            this.setData({
                sort: options.sort
            })
            //浏览记录
            if (wx.getStorageSync('token') && options.sort == 'add_time') {
                let data = {
                    type: 5,
                    client: 2
                }
                common.post('Userbrowse/add_user_browse', data).then(function(res) {}).catch(function(e) {})
            }
        }
        this.setData({
            keyWord: options.keyWord
        })
        this.getShareImg()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        this.getGoodsList()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {
        common.refresh()
        setTimeout(function() {
            wx.stopPullDownRefresh()
        }, 2000)
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {
        this.getGoodsList()
    },

    onShareAppMessage: function(res) {
        let userId = wx.getStorageSync('userId')
        let storeId = wx.getStorageSync('storeId')
        let keyWord = this.data.keyWord
        let sort = this.data.sort
        let shareImg = this.data.shareImg
        if (userId) {
            return {
                title: 'VIP会员服务隆重上线，超级福利来袭!',
                path: '/pages/search/searchList/searchLIst?keyWord=' + keyWord + '&sort=' + sort + '&userShare=' + userId,
                imageUrl: shareImg
            }
        }
    },

    getShareImg: function() {
        const that = this
        let data = {
            activity: 785,
            images_id: 10
        }
        common.post('Images/get_img', data).then(function(res) {
            that.setData({
                shareImg: res[0].images
            })
        }).catch(function(e) {})
    },

    getGoodsList: function() {
        if (this.data.isLoading) {
            return
        }
        if (this.data.isAll) {
            return
        }
        const that = this
        let data
        let goodsList = this.data.goodsList
        this.setData({
            isLoading: true
        })
        wx.showLoading({
            title: '加载中',
        })
        if (this.data.sort) {
            data = {
                order_by: this.data.sort,
                keyword: this.data.keyWord,
                limit: 10,
                page: this.data.nowPage,
                client: 1
            }
        } else {
            data = {
                keyword: this.data.keyWord,
                limit: 10,
                page: this.data.nowPage,
                client: 1
            }
        }
        common.post('Goodslist/goods_list', data).then(function(res) {
            that.setData({
                isLoading: false
            })
            wx.hideLoading()
            if (res.code == 204) {
                that.setData({
                    goodsList: goodsList,
                    isAll: true
                })
            } else {
                goodsList = goodsList.concat(res)
                that.setData({
                    goodsList: goodsList,
                    nowPage: that.data.nowPage + 1
                })
            }
        }).catch(function(e) {})
    },

    goGoodsDetail: function(e) {
        wx.navigateTo({
            url: '/pages/goods/goodsDetail/goodsDetail?goodsId=' + e.currentTarget.dataset.id,
        })
    }
})